// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import { UINames } from "../../Enums/UIEnum";
import UIManager from "../../Managers/UIManager";
import CharacterTemplate from "../../Models/Characters/CharacterTemplate";
import FuBenManager from "../../Models/FuBens/FuBenManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FuBenItem extends cc.Component {

    @property(cc.Sprite)
    icon: cc.Sprite = null;
    @property(cc.Label)
    rewardTxt: cc.Label = null;
    @property(cc.Button)
    challengeBtn: cc.Button = null;

    characterTp: CharacterTemplate;

    drawItem(tp: CharacterTemplate) {
        this.characterTp = tp;
        this.rewardTxt.string = "奖励：" + this.characterTp.reward + "金币";
        cc.resources.load("enemys/" + this.characterTp.id, cc.SpriteFrame, (error: Error, asset: cc.SpriteFrame) => {
            this.icon.spriteFrame = asset;
        });
        this.challengeBtn.interactable = FuBenManager.single.getFuBenCount() > 0;
    }

    challengeClick() {
        if (FuBenManager.single.getFuBenCount() <= 0) {
            UIManager.single.showTips("今日悬赏挑战次数已满。");
            return;
        }
        UIManager.single.closeUI(UINames.FuBenUI);
        FuBenManager.single.requestChallenge(this.characterTp);

    }
}
